GBML manual / Strings

string_replace

Every occurrence of target inside s, replaced with replacement - replacing text.

Syntax

string_replace(s, target, replacement) -> string

Arguments

NameType
sstring
targetstring
replacementstring

Returns

string

Description

Every occurrence of target inside s, replaced with replacement - replacing text.

An empty target matches nowhere and returns s unchanged (there is no single sensible "between every character" count). This is the most expensive function in the Strings group - it re-scans the remaining text after every match - fine for building one line of dialogue, not something to call unconditionally every frame with a long string.

Example

var line = "Press {BUTTON} to jump";
var shown = string_replace(line, "{BUTTON}", "A");

If replacement is longer than target, the result can grow past s's own length; like every string-building function in this group, whatever does not fit your project's own Longest string setting is dropped from the end - check string_length on the result if that matters to you.

On the GBA

An empty target matches nowhere and returns s unchanged (there is no single sensible "between every character" count). Costs the most of any function in this file - it re-scans the remaining tail after every match - fine for building one line of dialogue, not something to call unconditionally every frame with a long string. If replacement is longer than target, the result can grow past s's own length; like every string-building function here, whatever does not fit capacities.maxStringLength is silently dropped from the end - check string_length on the result if that matters to you.

See also: string_findstring_join